﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;

namespace PlanningGIS.ArcGIS
{
    /// <summary>
    /// 版本操作类
    /// </summary>
    public class VersionHelper
    {
        /// <summary>
        /// 创建版本
        /// </summary>
        /// <param name="pWs"></param>
        /// <param name="parentName"></param>
        /// <param name="newVersionName"></param>
        /// <returns></returns>
        public static IVersion2 CreateVersion(IWorkspace pWs, string parentName, string newVersionName)
        {
            if (!(pWs is VersionedWorkspace)) return null;
            IVersionedWorkspace3 pVersionedWs = pWs as IVersionedWorkspace3;
            IVersion2 pVersion = pVersionedWs.FindVersion(parentName) as IVersion2;
            return pVersion.CreateChild(newVersionName, pVersion) as IVersion2;
        }

        /// <summary>
        /// 创建Default的子版本
        /// </summary>
        /// <param name="pWs"></param>
        /// <param name="newVersionName"></param>
        /// <returns></returns>
        public static IVersion2 CreateDefaultChildVersion(IWorkspace pWs, string newVersionName)
        {
            if (!(pWs is VersionedWorkspace)) return null;
            IVersionedWorkspace3 pVersionedWs = pWs as IVersionedWorkspace3;
            IVersion2 pDefaultVersion = pVersionedWs.DefaultVersion as IVersion2;
            return pDefaultVersion.CreateChild(newVersionName, pDefaultVersion) as IVersion2;
        }

        /// <summary>
        /// 删除版本
        /// </summary>
        /// <param name="wks"></param>
        /// <param name="versionName"></param>
        /// <returns></returns>
        public static bool DeleteVersion(IWorkspace wks, string versionName)
        {
            if (!(wks is VersionedWorkspace)) return false;
            IVersionedWorkspace3 versionedWs = wks as IVersionedWorkspace3;
            IVersion2 version = versionedWs.FindVersion(versionName) as IVersion2;
            IVersionInfo versionInfo = null;
            try
            {
                IEnumVersionInfo pEnumVersInfo = versionedWs.Versions;
                versionInfo = pEnumVersInfo.Next();
                while (versionInfo != null)
                {
                    if (versionInfo.Description == "可删除")
                    {
                        IVersion2 version2 = versionedWs.FindVersion(versionInfo.VersionName) as IVersion2;
                        if (version2 != null) version2.Delete();
                    }
                    versionInfo = pEnumVersInfo.Next();
                }
            }
            catch 
            {
                
            }
            if (version != null)
            {
                version.Description = "可删除";
            }
            try
            {
                if (version != null)
                {
                    versionInfo = version.VersionInfo;
                    version.Delete();
                }
                return true;
            }
            catch 
            {
                return false;
            }
        }

        /// <summary>
        /// 发送到目标版本
        /// </summary>
        /// <param name="wks"></param>
        /// <param name="versionName"></param>
        /// <returns></returns>
        public static bool PostToTargetVersion(IWorkspace wks, string versionName)
        {
            if (!(wks is VersionedWorkspace)) return false;
            IVersionedWorkspace3 pVersionedWs = wks as IVersionedWorkspace3;
            IVersion2 pVersion = pVersionedWs.FindVersion(versionName) as IVersion2;
            IVersionEdit4 versionEdit = (IVersionEdit4)pVersion;
            ITransactions pTrans = wks as ITransactions;
            if (pTrans.InTransaction)
                pTrans.CommitTransaction();
            string targetVersionName =WorkspaceHelper.GetConnectProperty(wks, "VERSION");
            if (!versionEdit.Reconcile4(targetVersionName, true, false, false, true))
            {
                if (versionEdit.CanPost())
                    versionEdit.Post(targetVersionName);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 注册版本
        /// </summary>
        /// <param name="pWs"></param>
        /// <param name="pFeatDs"></param>
        /// <param name="newVersionName"></param>
        /// <returns></returns>
        public static IWorkspace RegisterAndCreateDefaultVersion(IWorkspace pWs, IFeatureDataset pFeatDs, string newVersionName)
        {
            if (pWs == null || String.IsNullOrEmpty(newVersionName)) return null;

            //首先如果入的库体是SDE的话则创建版本
            bool IsRegistered;
            bool IsMovingEditsToBase;
            if (pWs.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace && pFeatDs != null)
            {
                IVersionedObject3 versionObj = pFeatDs as IVersionedObject3;
                versionObj.GetVersionRegistrationInfo(out IsRegistered, out IsMovingEditsToBase);
                if (!IsRegistered)
                {
                    versionObj.RegisterAsVersioned3(false);
                }
                return (IWorkspace)CreateDefaultChildVersion(pWs, newVersionName);
            }
            return null;
        }
    
    }
}
